package com.joe.blockview.utils;


import android.content.res.Resources;
import android.util.Log;

import com.joe.blockview.model.Connection;
import com.joe.blockview.ui.BlockView;

import java.util.ArrayList;
import java.util.List;

public class ConnectManager {

    /**
     * start-0
     * end--1
     * normal-1
     * if-2
     * repeat-3
     * other-4
     */

    //根据坐标获取向上连接-----有可能连接的块
    public Connection findCloseUpConnection(List<Connection> connectionList, Resources resources, Connection connection) {
        if (connection.getType() == 0) {
            return null;
        }
        for (Connection connection1 :
                connectionList) {
            float dx = Math.abs(connection1.getWorkspacePoint().x - connection.getWorkspacePoint().x);
            float dy = 0;
            if (connection1.getType() == -1 || connection1.getType() == 1 || connection1.getType() == 0) {
                dy = connection.getWorkspacePoint().y - (connection1.getWorkspacePoint().y + 130);
            } else if (connection1.getType() == 2 || connection1.getType() == 3) {
                dy = connection.getWorkspacePoint().y - (connection1.getWorkspacePoint().y + (230 + connection1.getTagBlockView().getBLength()));
            } else {
                dy = 1000;
            }

            if (dx >= 0 && dy >= -20 && dx <= connection.getTagBlockView().getSelfWidth()
                    && dy <= connection.getTagBlockView().getSelfHeight()
                    && connection1.getType() != -1
                    && connection != connection1 && connection1.canConnect())
                return connection1;
        }
        return null;
    }

    //根据坐标获取向下连接-----有可能连接的块
    public Connection findCloseDownConnection(List<Connection> connectionList, Resources resources, Connection connection) {
        if (connection.getType() == -1) {
            return null;
        }
        for (Connection connection1 :
                connectionList) {
            float dx = Math.abs(connection1.getWorkspacePoint().x - connection.getWorkspacePoint().x);
            float dy = 0;
            if (connection.getType() == 0 || connection.getType() == 1) {
                dy = connection1.getWorkspacePoint().y - (130 + connection.getWorkspacePoint().y);
            } else if (connection.getType() == 2 || connection.getType() == 3) {
                dy = connection1.getWorkspacePoint().y - ((230 + connection.getTagBlockView().getBLength()) + connection.getWorkspacePoint().y);
            } else {
                dy = 1000;
            }

            if (connection != connection1) {

                if (dx >= 0 && dy >= -20 && dx <= connection.getTagBlockView().getSelfWidth()
                        && dy <= connection.getTagBlockView().getSelfHeight()
                        && connection1.getType() != 0
                        && connection1.getPreCanConnect()) {
                    return connection1;
                }
            }

        }
        return null;
    }

    //获取可用的输入连接
    public Connection findInputConnection(List<Connection> connectionList, Resources resources, Connection connection) {
        for (Connection connection1 :
                connectionList) {
            float dx = 0;
            float dy = 0;
            if (connection1.getType() == 2) {
                dx = Math.abs(connection1.getWorkspacePoint().x + 180 - connection.getWorkspacePoint().x);
                dy = Math.abs(connection1.getWorkspacePoint().y + 25 - connection.getWorkspacePoint().y);
            } else {
                dx = 1000;
                dy = 1000;
            }

            if (dx <= connection.getTagBlockView().getSelfWidth() && dy <= connection.getTagBlockView().getSelfHeight() && connection1.getType() != 0
                    && connection != connection1 && connection1.getInputCanConnect())
                return connection1;
        }
        return null;
    }

    //根据坐标获取内部开头向下连接
    public Connection findNextDownConnection(List<Connection> connectionList, Resources resources, Connection connection) {
        if (connection.getType() == 0) {
            return null;
        }
        for (Connection connection1 :
                connectionList) {
            float dx = 0;
            float dy = 0;
            if (connection1.getType() == 2 || connection1.getType() == 3) {
                dx = Math.abs(connection1.getWorkspacePoint().x + 20 - connection.getWorkspacePoint().x);
                dy = Math.abs(connection1.getWorkspacePoint().y + 130 - connection.getWorkspacePoint().y);
            } else {
                dy = 100;
            }

            if (dx < 30 && dy < 30 && connection1.getType() != -1
                    && connection != connection1 && connection1.getNextConnect())
                return connection1;
        }
        return null;
    }


    public Connection getLastBlockView(Connection connection) {
        Connection tag = connection;
        Connection lastTag = null;
        while (tag != null) {
            lastTag = tag;
            tag = tag.getTagConnection();
        }
        return lastTag;
    }


    public int getWidgetHight(List<Connection> connectionList) {
        int height = 0;
        for (Connection con :
                connectionList) {
            height = height + con.getTagBlockView().getBLength() + 230;
        }
        return height;
    }

    public List<Connection> filterConnnection(List<Connection> connectionList, Connection connection) {
        connectionList.remove(connection);
        if (connection.getNextConnection() != null) {
            filterConnnection(connectionList, connection.getNextConnection());
        }

        if (connection.getTagConnection() != null) {
            filterConnnection(connectionList, connection.getTagConnection());
        }
        return connectionList;
    }

}
